var tableIns;
layui.extend({
    dtree:'layui_ext/dist/dtree'
}).use([ 'jquery', 'layer', 'form', 'table'  ], function() {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    //渲染数据表格
    tableIns=table.render({
        elem: '#paibanTable'   //渲染的目标对象
        ,url:'departmentCenter/findAllPaiban' //数据接口
        ,title: '用户数据表'//数据导出来的标题
        ,height:'full-170'
        ,page: 1  //是否启用分页, limits: [10, 20, 30]
        ,limits: [10, 20, 30]
        ,limit: 10//默认采用10
        ,cols: [[   //列表数据
            {field:'doctorId', title:'ID',align:'center',width:'10%',sort:true}
            ,{field:'doctorName', title:'姓名',align:'center',width:'10%'}
            ,{field:'one', title:'星期一',align:'center',width:"10%",templet:function(d){
                    if(d.one==null){
                        return "无班";
                    }else{
                        return d.one;
                    }
                }}
            ,{field:'two', title:'星期二',align:'center',width:"10%",templet:function(d){
                    if(d.two==null){
                        return "无班";
                    }else{
                        return d.two;
                    }
                }}
            ,{field:'three', title:'星期三',align:'center',width:"10%",templet:function(d){
                    if(d.three==null){
                        return "无班";
                    }else{
                        return d.three;
                    }
                }}
            ,{field:'four', title:'星期四',align:'center',width:"10%",templet:function(d){
                    if(d.four==null){
                        return "无班";
                    }else{
                        return d.four;
                    }
                }}
            ,{field:'five', title:'星期五',align:'center',width:"10%",templet:function(d){
                    if(d.five==null){
                        return "无班";
                    }else{
                        return d.five;
                    }
                }}
            ,{field:'six', title:'星期六',align:'center',width:"10%",templet:function(d){
                    if(d.six==null){
                        return "无班";
                    }else{
                        return d.six;
                    }
                }}
            ,{field:'seven', title:'星期日',align:'center',width:"10%",templet:function(d){
                    if(d.seven==null){
                        return "无班";
                    }else{
                        return d.seven;
                    }
                }}
            ,{fixed: 'right', title:'操作', toolbar: '#paiban',align:'center',width:'10%'}
        ]]
    })


    //模糊查询
    $("#doSearch").click(function(){
        var params=$("#searchFrm").serialize();
        tableIns.reload({
            url:"departmentCenter/findAllPaiban?"+params
        })
    });
    $.ajax({
        url: 'departmentCenter/findAllBan',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            $.each(data, function (index, item) {
                $('.bid').append(new Option(item.bname, item.bname));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }
    })
    //监听行工具事件
    table.on('tool(paibanTable)', function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if(layEvent === 'del'){ //删除
            layer.confirm('真的删除【'+data.doctorName+'】这个医生吗', function(index){
                //向服务端发送删除指令
                $.post("departmentCenter/deleteDoctor",{doctorId:data.doctorId},function(res){
                    layer.msg(res);
                    //刷新数据 表格
                    tableIns.reload();
                })
            });
        } else if(layEvent === 'edit'){ //编辑
            openUpdatePaiban(data);
        }
    });
    var url;
    var mainIndex;
    //打开修改页面
    function openUpdatePaiban(data){
        mainIndex=layer.open({
            type:1,
            title:'修改排班',
            content:$("#saveOrUpdateDiv"),
            area:['500px','600px'],
            success:function(index){
                form.val("dataFrm",data);
                url="departmentCenter/editPaiban";
            }
        });
    }
    //保存
    form.on("submit(doSubmit)",function(obj){
        //序列化表单数据
        var params=$("#dataFrm").serialize();
        $.post(url,params,function(obj){
            layer.msg(obj);
            //关闭弹出层
            layer.close(mainIndex)
            //刷新数据 表格
            tableIns.reload();
        })
    });
});